package com.gxh.controller.user;

import com.gxh.dto.UserMachinePageQueryDTO;
import com.gxh.result.PageResult;
import com.gxh.result.Result;
import com.gxh.service.MachineService;
import com.gxh.vo.MachineDetailVO;
import com.gxh.vo.MachineItemVO;
import com.gxh.vo.MachineKWResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("UserMachineController")
@RequestMapping("/user/machine")
@Slf4j
@Api(tags = "用户端设备接口")
public class MachineController {

    @Autowired
    private MachineService machineService;

    /**
     * 用户端设备列表
     * @return
     */
    @RequestMapping("/page")
    @ApiOperation("用户端设备列表")
    public Result<PageResult> page(UserMachinePageQueryDTO userMachinePageQueryDTO){
        log.info("用户端设备列表{}" , userMachinePageQueryDTO);
        PageResult pageResult = machineService.userPageQuery(userMachinePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 用户端设备搜索
     * @return
     */

    @GetMapping("/search")
    @ApiOperation("用户端设备搜索")
    @Cacheable(value = "machineKWCache",key = "'search:' + #kw")
    public Result<List<MachineKWResultVO>> search( String kw){
        log.info("用户端设备搜索{}" ,kw);
        List<MachineKWResultVO> machineKWResultVOList = machineService.search(kw);
        return Result.success(machineKWResultVOList);
    }

    /**
     * 用户端设备详情
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("用户端设备详情")
    public Result<MachineDetailVO> getById(@PathVariable Long id){
        log.info("用户端设备详情{}" ,id);
        MachineDetailVO machineDetailVO = machineService.userGetById(id);
        return Result.success(machineDetailVO);
    }


    /**
     * 用户端最新设备
     * @return
     */
    @GetMapping("/new")
    @ApiOperation("用户端最新设备")
    @Cacheable(value = "machineCache",key = "'new'")
    public Result<List<MachineItemVO>> getNew(){
        List<MachineItemVO> machineItemVOS = machineService.getNew();
        return Result.success(machineItemVOS);
    }

    /**
     * 用户端推荐设备
     * @return
     */
    @GetMapping("/recommend")
    @ApiOperation("用户端推荐设备")
    @Cacheable(value = "machineCache",key = "'recommend'")
    public Result<List<MachineItemVO>> getRecommend(){
        List<MachineItemVO> machineItemVOS = machineService.getRecommend();
        return Result.success(machineItemVOS);
    }
    @GetMapping("/inFactory")
    @ApiOperation("用户端设备工厂在位")
    @Cacheable(value = "machineCache",key = "'inFactory'")
    public Result<List<MachineItemVO>> inFactory(){
        List<MachineItemVO> machineItemVOS = machineService.getInFactory();
        return Result.success(machineItemVOS);
    }
}
